﻿<?php
class GroupAction extends CommonAction{
    /*+--------------------------
      |   显示创建管理员页面
      +--------------------------
     */
    public function createGroup(){
           $this->display();
    }

    /*+--------------------------
      |    执行创建管理员
      +--------------------------*/
    public function addGroup(){
           if($_POST['create']){
                   $group = D("group");
                   $data['group_name']     = $_POST['group_name'];
                   $data['group_describe'] = $_POST['group_describe'];
                   $data['create_time']    = toDate(time());
                   $data['create_user']    = $_SESSION[C("USER_AUTH_KEY")];
                   //echo "<pre>";
                   //print_r($data);
                   $add   = $group->add($data);
                   if($add){
                        //echo $add;
                       //exit;
                        $this->success("创建角色成功！");
                   }else{
                        $this->error("创建角色失败！");
                   }
           }
    }
    
    /*+--------------------------
      |    查看管理员
      +--------------------------
     */
    public function showGroup(){
        //显示所有的用户组:
           $order = "create_time desc";
           $limit = $this->pageOperate("group","15");
           $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows']));
           $this->display();
    }

	/*+------------------------------
	  |       显示该分组下的用户信息
	  +------------------------------
	*/
	public function showAdmin(){
		   if($_GET['Gid']){
			       $order = "create_time desc";
             	   $limit = $this->pageOperate("sysuser","15");
			 	   $query = "select a.*,b.sysuser_nikename create_user,a.create_time from douhao_sysuser a,douhao_sysuser b where a.create_user = b.id and a.Gid = {$_GET['Gid']}";
             	   $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows'],$query));  
		   }
		   $this->display();
    }
	
	/*+------------------------------
	  |       管理管理员
	  +------------------------------
	  */
	 public function showFunction(){
		     $func = D("group");
			 $group_function = $func->where("id = {$_GET['Gid']}")->find();
			 $in_array = explode(",",$group_function['group_function']);
			 
		     $param['after']  = array(
									  'content'=>'&nbsp;<input type="checkbox" CK />&nbsp;&nbsp;',
									  'explode'=>array(
													   '0'=>array(
															      'PK'  =>'CK',
																  'swap'=>'checked',
															 ),
													   ),
									  'in_array' => $in_array,
									  );
        	 $param['link']   = "0";
        	 $manageFunction = $this->get_menu(0,$param);
			 $this->assign("manageFunction",$manageFunction);
		     $this->display();
		 }
     
	/*+-------------------------------
	  |   管理功能
	  +-------------------------------
	  */
	 public function editFunction(){
	         $func = D("group");
			 $group_function = $func->where("id = {$_GET['Gid']}")->find();
			 $in_array = explode(",",$group_function['group_function']);
			 
		     $param['after']  = array(
									  'content'=>'&nbsp;<input type="checkbox" CK name="id[]" value="PK"/>&nbsp;&nbsp;',
									  'explode'=>array(
													   '0'=>array(
															      'PK'  =>'CK',
																  'swap'=>'checked',
															 ),
													   '1'=>array(
																  'PK'  =>'PK',
																  'field'=>'id',
															 ),
													   
													   ),
									  'in_array' => $in_array,
									  );
        	 $param['link']   = "0";
        	 $manageFunction = $this->get_menu(0,$param);
			 $this->assign("manageFunction",$manageFunction);
			 $this->assign("Gid",$_GET['Gid']);
		     $this->display();
		 }
	 /*+-------------------------------
	   |    赋予角色功能
	   +-------------------------------
	   */
	  public function saveFunction(){
	         if($_POST['save']){
			       foreach($_POST['id'] as $key=>$val){
				        $str .= $val.",";
				   }
				   $str = rtrim($str,",");
				   $saveFun = D("group");
				   $data['id']             = $_POST['Gid'];
				   $data['group_function'] = $str;
				   $update  = $saveFun ->save($data);
				   if($update){
				          $this->success("角色功能编辑完成！");
				   }else{
				          $this->error("角色功能编辑失败！");
				   }
			 }
	  }
	  
	  /*+---------------------------------------------
	    |    管理角色中的用户
		+---------------------------------------------
		*/
	  public function editAdmin(){
	         $order = "create_time desc";
             $limit = $this->pageOperate("sysuser","15");
			 $query = "select a.*,b.sysuser_nikename create_user,a.create_time from douhao_sysuser a,douhao_sysuser b where a.create_user = b.id";
             $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows'],$query),$_GET['Gid']);
             $this->assign("Gid",$_GET['Gid']); 
	         $this->display();
	  }
	  /*+---------------------------------------------
	    |    执行修改角色中的管理员
		+---------------------------------------------
		*/
	  public function executeEditAdmin(){
	         $admin  = D("sysuser");
	         if($_GET['add']){
			       $where = "id = {$_GET['add']}";
			 }
			 if($_GET['del']){
			       $where        = "id = {$_GET['del']}";
				   $data["Gid"]  = 0; 
				   $update = $admin -> where($where)->save($data);
				   if($update){
				        $this->success("移出管理员成功！");
				   }else{
				        $this->error("移出管理员失败！");
				   }
			 }
			 if($_POST['save']){
			       foreach($_POST['id'] as $key => $val){
				       $in .= $val.",";
				   }
				   $in    = rtrim($in,",");
				   $where = "id in ({$in})"; 
			 }
			 
			 if($_POST['id']){
			 		$data['Gid'] = $_POST['Gid'];
			 }
			 if($_GET['Gid']){
			        $data['Gid'] = $_GET['Gid'];
			 }
			 if($_POST['save'] || $_GET['add']){
			       $update = $admin -> where($where)->save($data);
				   if($update){
				           $this->success("成功的添加了 {$update} 个组成员！");
				   }else{
				           $this->error("添加成员失败！");
				   }
			 }
			 
			  
	  }
	/*+--------------------------------
	  |   修改角色信息
	  +--------------------------------
	  */
	 public function executeEdit(){
		     $group = D("group");
			 if($_GET['Gid']){
				     $where = "id = {$_GET['Gid']}";
				 }
			 if($_POST['edit']){
				     $where = "id in (".$this->arrayToString($_POST['id']).")";
				 }
				  //$where .= "";
		     if($_POST['save']){
				  $editArray = $this->arraySwitch($_POST);
				  foreach($editArray as $key => $val){
					     $edit += $group->save($val);
			      }
				  if($edit){
					       $this->success("成功的修改了 {$edit} 个角色信息!");
				  }else{
						   $this->error("修改角色信息失败！");
				  }
			 }
				 
			
			 $edit = $group ->where($where)->select();
			 $this->assign("GroupAdmin",$edit);
			 $this->display();
		 }
    /*+--------------------------------
      |   编辑管理员
      +--------------------------------
     */
    public function editGroup(){
		   $order = "create_time desc";
           $limit = $this->pageOperate("group","15");
           $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows']));
           $this->display();
    }

    /*+--------------------------------
      |   删除管理员
      +--------------------------------
     */

    public function delGroup(){
		   $order = "create_time desc";
           $limit = $this->pageOperate("group","15");
           $this->getGroupInfo($this->query($order,$limit['firstRow'].",".$limit['listRows']));
           $this->display();
    }
	
	/*+--------------------------------
	  |    执行删除操作
	  +--------------------------------
	  */
	 public function executeDel(){
		     if($_GET['Gid']){
				      $where = "id = {$_GET['Gid']}";
				 }
		     if($_POST['del']){
				      $where = "id in (".$this->arrayToString($_POST['id']).")";
				 }
			 $group = D("group");
		     $del   = $group -> where($where) ->delete();
			 if($del){
				     $this->success("成功的删除了 {$del} 个角色!");
			 }else{
				     $this->error("删除角色失败！");
				 }
		 }

    //多表查询语句、
    public function query($order,$limit,$query){
	    if($query){
		       $query = $query;
		}else{
              $query = "select a.id,a.group_name,a.group_describe,b.sysuser_nikename create_user,a.create_time from douhao_group a,douhao_sysuser b where a.create_user = b.id";
	    }
		
        if($order){
             $query .= " order by a.".$order;

        }
        if($limit){
            $query .= " limit ".$limit;
        }
        return $query;

    }
	
}

?>
